//
//  MYGCloudDiskValidityPeriodSheet.swift
//  MYDoctor
//
//  Created by Denny on 2024/3/13.
//

import UIKit

class MYGCloudDiskValidityPeriodSheet: UIView {
    var dataSource = [MYCloudDiskValidityPeriodModel]()
    @IBOutlet weak var tableView: UITableView!
    var tapClosePageCompletion:(()->Void)?
    var tapIndexCompletion:((Int,MYCloudDiskValidityPeriodModel)->Void)?

    func setupDataSource() {
        self.dataSource.append(configModel("7天内有效", "分享将在7天后失效，失效后无法继续访问"))
        self.dataSource.append(configModel("30天内有效", "分享将在30天后失效，失效后无法继续访问"))
        self.dataSource.append(configModel("自定义到期时间", "可设定30天内任意日期"))
        self.dataSource.append(configModel("永久有效", "在手动取消前，分享将会持续有效"))
        self.tableView.reloadData()
    }
    
    func configModel(_ title:String,_ subtitle:String,selected:Bool = false) ->MYCloudDiskValidityPeriodModel{
        let model = MYCloudDiskValidityPeriodModel()
        model.title = title
        model.subTitle = subtitle
        model.selected = selected
        return model
    }
    
    @IBAction func tapCloseBtn(_ sender: Any) {
        if let cc = self.tapClosePageCompletion {
            cc()
        }
    }
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.tableView.register(MYGCloudDiskValidityPeriodCell.self, forCellReuseIdentifier: "MYGCloudDiskValidityPeriodCell")
        self.tableView.dataSource = self
        self.tableView.delegate = self
        setupDataSource()
    }
}

extension MYGCloudDiskValidityPeriodSheet:UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "MYGCloudDiskValidityPeriodCell") as! MYGCloudDiskValidityPeriodCell
        let model = self.dataSource[indexPath.row]
        cell.bindData(model)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let model = self.dataSource[indexPath.row]
        if let c = tapIndexCompletion {
            c(indexPath.row,model)
        }
    }
    
}

class MYGCloudDiskValidityPeriodCell: UITableViewCell {
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        addSubview(actionBtn)
        actionBtn.snp.makeConstraints { make in
            make.size.equalTo(CGSize.init(width: 35, height: 35))
            make.left.equalToSuperview().offset(15)
            make.centerY.equalToSuperview()
        }
        addSubview(titleLB)
        titleLB.snp.makeConstraints { make in
            make.top.equalToSuperview()
            make.left.equalTo(actionBtn.snp.right).offset(14)
        }
        addSubview(subLB)
        subLB.snp.makeConstraints { make in
            make.top.equalTo(titleLB.snp.bottom).offset(4)
            make.left.equalTo(actionBtn.snp.right).offset(14)
        }
    }
    
    func bindData(_ data: MYCloudDiskValidityPeriodModel) {
        self.titleLB.text = data.title
        self.subLB.text = data.subTitle
        self.actionBtn.isSelected = data.selected
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    var titleLB:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#2C2C2C")
        control.font = UIFont.medium(14)
        return control
    }()
    
    var subLB:UILabel = {
        let control = UILabel()
        control.textColor = UIColor.init(hexString: "#808080")
        control.font = UIFont.regular(11)
        return control
    }()
    
    lazy var actionBtn: UIButton = {
        let control = UIButton()
        control.setImage(UIImage.init(named: ""), for: .normal)
        control.setImage(UIImage.init(named: ""), for: .highlighted)
        control.setImage(UIImage.init(named: ""), for: .selected)
        control.addTarget(self, action: #selector(tapBox(_ :)), for: .touchUpInside)
        return control
    }()
    
    @objc func tapBox(_ sender:UIButton) {
        
    }
}


